<?php

if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class hotmovie {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['params']['1'];
		require_once(APP_PATH."model/admin/init.php" );
		if($action == 'add'){
			$this->addAction();
			exit();
		}elseif($action == 'del'){
			$this->delAction();
			exit();
		}elseif($action == 'edit'){
			$this->editAction();
			exit();
		}elseif($action == 'getmovies'){
			$this->getMovieList();
			exit();			
		}else{
			$this->defaultAction();
			exit();
		}
	}	

	function defaultAction(){
		$tpl = new Template( "admin/hotmovie/list.htm" );
		$db  = getDB();
		$cache = DATA_PATH."cache/hotmovies.php";
		if(is_file($cache)){
			$movies = require_once($cache);
		}
		if($movies){
			foreach($movies as $k=>$v){
				$mids .= "'".$v['id']."',";
			}
			$mids = substr($mids,0,-1);
		}
		//de($movies);
		foreach($movies as $k=>$v){
			$movies[$k]['k'] = $k;
			$movies[$k]['playtime'] = date('Y-m-d',$v['playtime']);
			$movies[$k]['endtime'] = date('Y-m-d',$v['endtime']);
			$movies[$k]['addtime'] = date('Y-m-d H:i:s',$v['times']);
		}
		$tpl->assign('movieList', $movies);
		$tpl->output();
	}

	function addAction(){
		$db = getDB();
		$cache = DATA_PATH."cache/hotmovies.php";
		if(is_file($cache)){
			$movies = require_once($cache);
		}
		if(!empty($movies)){
			$s = count($movies);			
			if(count($movies) >= 4){
				message('请不要增加超过4条的数据。');
			}
		}
		if(!isPost()){
			$tpl = new Template("admin/hotmovie/add.htm");
			$tpl->output();
		}else{			
			$movieid = trim($_POST['movieid']);
			$sql = "SELECT id,title,playtime,endtime FROM movie WHERE id = '$movieid' ";
			$db->query($sql);
			$getMovie = $db->fetchRow();
			if(empty($getMovie)){
				message('没有这部电影');
			}			
			if(!empty($movies)){
				foreach($movies as $k=>$v){
					if($v['id'] == $movieid){
						message('这部电影已增加');
					}
				}
			}
			$getMovie['times'] = time();
			$movies[] = $getMovie;
			$add = saveArray(DATA_PATH.'cache/hotmovies.php',$movies);			
			if ($add){
				$message = '成功增加一条热映中电影';					
			}else{
				$message = '增加一条热映中电影失败';
			}
			message($message);
		}
	}
	
	function editAction(){
		$db = getDB();		
		if(!isPost()){
			$tpl = new Template("admin/hotmovie/edit.htm");
			$url = getUrl();
			$var['editid'] = $url['params']['2'];
			$tpl->assign($var);
			$tpl->output();
		}else{
			$editid = trim($_POST['editid']);
			$movieid = trim($_POST['movieid']);
			$sql = "SELECT id,title,playtime,endtime FROM movie WHERE id = '$movieid' ";
			$db->query($sql);
			$getMovie = $db->fetchRow();
			if(empty($getMovie)){
				message('没有这部电影');
			}
			$cache = DATA_PATH."cache/hotmovies.php";
			if(is_file($cache)){
				$movies = require_once($cache);
			}
			if(!empty($movies)){
				$getMovie['times'] = time();
				foreach($movies as $k=>$v){
					if($k == $editid){
						$movies[$k] = '';
						$movies[$k] = $getMovie;
					}
				}
			}
			$add = saveArray(DATA_PATH.'cache/hotmovies.php',$movies);			
			if ($add){
				$message = '成功修改热映中电影';					
			}else{
				$message = '修改热映中电影失败';
			}
			message($message);
		}
	}

	function delAction(){
		$movieid = $this->url['params']['2'];
		if(!is_numeric($movieid)){
			error('找不到此条数据。');
		}
		$cache = DATA_PATH."cache/hotmovies.php";
		if(is_file($cache)){
			$movies = require_once($cache);
		}
		if($movies){
			foreach($movies as $k=>$v){
				if($k == $movieid){
					$delid = $k;
				}
			}
			array_splice($movies,$delid,1);
			$add = saveArray(DATA_PATH.'cache/hotmovies.php',$movies);
			if($add){
				$message = '删除热映中电影数据成功。';
			}else{
				$message = '删除热映中电影数据失败！';
			}
			message($message);
		}else{
			message('热映中电影数据不存在。');
		}
			
	}
	
	function getMovieList (){
		$q = strtolower($_GET["q"]);
		if (!$q) return;
		$db =  getDB();
		$sql = "SELECT id,title FROM movie WHERE title LIKE '%$q%' ";
		$db->query($sql);
		$items = $db->fetchAll();
		foreach ($items as $key => $value) {
			if (strpos(strtolower($value['title']), $q) !== false) {
				echo $value['title'].'|'.$value['id']."\n";
			}
		}
		
	}	
}
